home *** CD-ROM | disk | FTP | other *** search
/ Chip 2005 March / CMCD0305.ISO / Software / Shareware / Utilitare / emu / Emu8086_Setup_307c.exe / {app} / Samples / factorial.asm < prev    next >
Assembly Source File  |  2002-08-02  |  1KB  |  88 lines

  1. ; This sample gets the number
  2. ; from the user, and calculates
  3. ; factorial for it.
  4. ; Supported input from 0 to 8
  5. ; inclusive!
  6.  
  7. #make_COM#
  8.  
  9. include 'emu8086.inc'
  10.  
  11. ORG     100h
  12.  
  13. start:
  14.  
  15. ; get first number:
  16.  
  17. CALL    PTHIS
  18. DB 13, 10, 'Enter the number: ', 0
  19.  
  20. CALL    scan_num
  21.  
  22.  
  23. ; factorial of 0 = 1:
  24. MOV     AX, 1
  25. CMP     CX, 0
  26. JE      print_result
  27.  
  28. ; move the number to BX:
  29. ; CX will be a counter:
  30.  
  31. MOV     BX, CX
  32.  
  33. MOV     AX, 1
  34. MOV     BX, 1
  35.  
  36. calc:
  37. MUL     BX
  38. CMP     DX, 0
  39. JNE     overflow
  40. INC     BX
  41. LOOP    calc
  42.  
  43. print_result:
  44.  
  45. ; print result in AX:
  46. CALL    PTHIS
  47. DB 13, 10, 'Factorial: ', 0
  48.  
  49. CALL    PRINT_NUM_UNS
  50. JMP     exit
  51.  
  52.  
  53. overflow:
  54. CALL    PTHIS
  55. DB 13, 10, 'The result is too big!', 13, 10, 'Use values from 0 to 8.', 0
  56. JMP     start
  57.  
  58. exit:
  59.  
  60. RET
  61.  
  62. ;=================================
  63. ; here we define the functions
  64. ; from emu8086.inc
  65.  
  66. ; SCAN_NUM reads a
  67. ; number from the user and stores
  68. ; it in CX register.
  69.  
  70. DEFINE_SCAN_NUM
  71.  
  72. ; PRINT_NUM prints a signed
  73. ; number in AX.
  74. ; PRINT_NUM_UNS prints an unsigned
  75. ; number in AX (required by 
  76. ; PRINT_NUM):
  77.  
  78. DEFINE_PRINT_NUM
  79. DEFINE_PRINT_NUM_UNS
  80.  
  81. ; PTHIS prints NULL terminated
  82. ; string defined just after
  83. ; the CALL PTHIS instruction:
  84. DEFINE_PTHIS
  85.  
  86. ;=================================
  87. END
  88.